<?php
/**
 * 毛文科
 */
namespace app\admin\controller;
use app\admin\model\getdatas\Mydata;
use app\admin\model\getdatas\MySubject;
use app\admin\model\Modular;
use app\admin\model\myshuju\MyHandle;
use app\admin\model\MyUpFile;
use app\admin\model\UpFile;
use app\BaseController;
use baidu\Baidu;
use think\facade\Db;
use think\Request;

class Api extends BaseController
{
    /**设置模块
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function set_modular(Request $request){
        $data = $request->param();
        $modular = config('modular.');
        foreach ($modular as $value){
            if($value['dir']==$data['dir']){
                echo '此模块已存在';
                exit;
            }
        }
        $modular = array_merge($modular,[$data]);
        if(Modular::changeConfig('',$modular)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }

    /**后台上传图片接口
     * @return \think\response\Json
     */
    public function upimg(Request $request){
//        $array = file_get_contents('php://input');
//        echo '<pre>';
//        print_r($array);
//        exit;
        $data = MyUpFile::addFiles();
//        return json($data);
        if($data){
            return json_to_respos('上传成功',200,$data);
        }else{
            return json_to_respos('上传失败',205);
        }
    }

    /**上传封面
     * @return \think\response\Json
     */
    public function upfenmian(Request $request){
        $id = $request->param('id',null);
        if($id){
            $data = MyUpFile::addFiles('图片',false,false);
            $data = $data[0];
            Db::name(getTable('enclosure'))
                ->where('id',$id)
                ->update(['fenmian'=>$data['path']]);
            $data['id'] = $id;
        }else{
            $data = MyUpFile::addFiles('图片',false,false);
            $data = $data[0];
            $data['create_time'] = time();
            $data['status'] = 1;
            $data['fenmian'] = $data['path'];
            $data['type'] = '视频';
            $data['id'] = Db::name(getTable('enclosure'))
                ->insertGetId($data);
        }
        if($data){
            return json_to_respos('上传成功',200,$data);
        }else{
            return json_to_respos('上传失败',205);
        }
    }

    /**视频上传视频
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function upvideo(Request $request){
        $id = $request->param('id',null);
//        return json($id);
        $data = MyUpFile::FenPians(false);
        if($data['wc']??null){
            if($id){
                $newdata['yuan_name'] = $data['name'];
                $newdata['new_name'] = $data['newname'];
                $newdata['quan_path'] = $data['path'];
                if(!strstr($data['path'],'http')){
                    $newdata['quan_path'] = request()->domain().$data['path'];
                }
                $newdata['path'] = $data['path'];
                Db::name(getTable('enclosure'))
                    ->where('id',$id)
                    ->update($newdata);
                $data['id'] = $id;
            }else{
                $newdata['yuan_name'] = $data['name'];
                $newdata['new_name'] = $data['newname'];
                $newdata['path'] = $data['path'];
                $newdata['type'] = '视频';
                $newdata['create_time'] = time();
                $newdata['status'] = 1;
                $newdata['quan_path'] = $data['path'];
                if(!strstr($data['path'],'http')){
                    $newdata['quan_path'] = request()->domain().$data['path'];
                }
                $newdata['id'] = Db::name(getTable('enclosure'))
                    ->insertGetId($newdata);
                $data['id'] = $newdata['id'];
            }
        }
        return json($data);
    }

    /**删除图片接口
     * @param Request $request
     * @return \think\response\Json
     */
    public function deleteimg(Request $request){
        $id = $request->param('id');
        if(UpFile::delete($id)){
            return json_to_respos('删除成功');
        }else{
            return json_to_respos('删除失败',204);
        }
    }

    /**编辑框编辑的图片上传
     * @param Request $request
     * @return \think\response\Json
     */
    public function editorimgs(Request $request){
        $datas = MyUpFile::addFiles('图片',false,false,'layui');
        $data['code'] = 0;
        $data['msg'] = '上传成功';
        $data['data'] = [
            "src"=>$datas[0]['path']
            ,"title"=>$datas[0]['yuan_name']
        ];
        return json($data);
    }

    /**分片上传图片
     * @param Request $request
     * @return \think\response\Json
     */
    public function fenpian(Request $request){
        $data = $request->param();
//        return json($data);
        $data = MyUpFile::FenPians(true,'文件');
        return json($data);
    }
    /**获取上传token
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addqiniufile(Request $request){
        $data = UpFile::addQiniu();
        return json($data);
    }

    /**后台启动翻译
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function fanyi(Request $request){
        sleep(1);
        $lang = $request->param('lang',null);
        if($lang=='zh'){
            return json_to_respos('已翻译完成',200);
        }
        if(config('fanyi')){
            $data = Db::name(getTable('lange'))
                ->where($lang,null)
                ->whereOr($lang,'')
                ->cursor();
            $number = 0;
            $str = '';
            $shuju = [];
            foreach ($data as $key=>$value){
                if(strlen($str)<5500){
                    $number++;
                    $str .= $value['zh'].'&';
                    $shuju[$key] = $value;
                }else{
                    break;
                }
            }
            if($number==0){
                return json_to_respos('已翻译完成',200);
            }
            $str = trim($str,'&');
            $obj = Baidu::Fanyi();
            $array = $obj->getFanyi($lang,$str);
            if($array){
                $array = explode('&',$array);
                foreach ($shuju as $key=>$value){
                    Db::name(getTable('lange'))
                        ->where('id',$value['id'])
                        ->update([$lang=>trim($array[$key])]);
                }
                return json_to_respos('未翻译完成',202);
            }else{
                return json_to_respos('出错',206,$obj->error);
            }
        }
        return json_to_respos('已翻译完成',200);
    }

    /**获取是否有消息
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function getadminmessage(Request $request){
        $data = Db::name(getTable('adminmessage'))
            ->where('status',0)
            ->count();
        if($data>0){
            return json_to_respos('获取成功',200,$data);
        }else{
            return json_to_respos('获取成功',202,$data);
        }
    }

    /**获取消息分页
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function getadminmessagelist(Request $request){
        $limit = $request->param('limit',5);
        $data = Db::name(getTable('adminmessage'))
            ->order('create_time desc')
            ->paginate($limit)
            ->toArray();
        foreach ($data['data'] as $key=>$value){
            $value['create_time'] = date("Y-m-d H:i:s",$value['create_time']);
            $data['data'][$key] = $value;
            Db::name(getTable('adminmessage'))
                ->where('id',$value['id'])
                ->update(['status'=>1]);
        }
        return json_to_respos('获取成功',200,$data);
    }
    public function shanchuduoyutupian(){
        set_time_limit(0);
        Db::name('enclosure')
            ->where('id','>',0)
            ->update([
                'status'=>0
            ]);
        $model = Db::name('models')
            ->where('status',1)
            ->where('type','<>',1)
            ->cursor();
        foreach ($model as $value){
            $this->changeimgmdoel($value['id']);
        }
        $date = Db::name('systems')
            ->where('tupian','in','单文件,多文件,单图片,多图片,视频')
            ->select()->toArray();
        foreach ($date as $value){
            if($value['value']){
                $this->changeimg($value['value'],$value['tupian']);
            }
        }
        $data = Db::name('enclosure')
            ->where('status',0)
            ->cursor();
        foreach ($data as $value){
            UpFile::delete($value['id'],true);
        }
        return json_to_respos('成功',200,$model);
    }
    public function changeimgmdoel($modelid){
        $model = Db::name('models')->where('id',$modelid)->find();
        $obj = MyHandle::GetConfig();
        if($model['type']=='联表'){
            $obj->setModelId($modelid);
            $field = Db::name('fields')->where('model_id',$modelid)
                ->where('form_type','in','单文件,多文件,单图片,多图片,视频')
                ->select()->toArray();
            $data = Db::name($model['table'])->where('id','>',0)
                ->cursor();
            foreach ($data as $value){
                foreach ($field as $va){
                    if(!empty($value[$va['field']])){
                        $this->changeimg($value[$va['field']],$va['form_type']);
                    }
                }
            }
        }elseif($model['type']=='单表'){
            $lanmu = Db::name('column')
                ->where('model_id',$modelid)
                ->select()->toArray();
            $lanmuid = [];
            foreach ($lanmu as $value){
                $lanmuid[] = $value['id'];
            }
            $field  = Db::name('ziduancid')
                ->where('cid','in',$lanmuid)
                ->where('form_type','in','单文件,多文件,单图片,多图片,视频')
                ->select()->toArray();

            $data = Db::name($model['table'])->where('id','>',0)
                ->cursor();
            foreach ($data as $value){
                foreach ($field as $va){
                    if(!empty($value[$va['field']])){
                        $this->changeimg($value[$va['field']],$va['form_type']);
                    }
                }
            }
//            echo '<pre>';
//            print_r($model);
//            exit;
        }
    }
    public function changeimg($id,$type){
//        echo $id.PHP_EOL;
        if($type=='单图片'||$type=='单文件'||$type=='视频'){
            Db::name('enclosure')
                ->where('id',$id)
                ->update(['status'=>1]);
        }else{
            Db::name('enclosure')
                ->where('id','in',$id)
                ->update(['status'=>1]);
        }
    }
}